﻿using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Mail;
using System.Net.Mime;
using System.Threading.Tasks;

namespace OnlineEducation.ExternalService
{
    public class EmailOptions
    {
        public string FromAddress { get; set; }

        public string FromDisplayName { get; set; }

        public bool BodyIsPlain { get; set; }

        public string UserName { get; set; }

        public string Password { get; set; }
    }

    public class EmailSender : IEmailSender
    {
        public EmailOptions Options { get; }
        public ILogger<EmailSender> _logger { get; }

        public EmailSender(IOptions<EmailOptions> optionsAccessor, ILogger<EmailSender> logger)
        {
            Options = optionsAccessor.Value;
            _logger = logger;
        }

        public Task SendEmail(string email, string subject, string message)
        {
            return Task.Run(() => Execute(subject, message, email));

        }

        public Task SendEmail(string email, string subject, string message, string receiver)
        {
            return Task.Run(() => Execute(subject, message, email, receiver));
        }

        private void Execute(string subject, string message, string email)
        {
            try
            {
                MailMessage mailMsg = new MailMessage();

                mailMsg.To.Add(new MailAddress(email));

                mailMsg.From = new MailAddress(Options.FromAddress, Options.FromDisplayName);

                mailMsg.Subject = subject;

                // 邮件正文内容
                if (Options.BodyIsPlain)
                {
                    mailMsg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(message, null, MediaTypeNames.Text.Plain));
                }
                else
                {
                    mailMsg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(message, null, MediaTypeNames.Text.Html));
                }

                //邮件推送的SMTP地址和端口
                SmtpClient smtpClient = new SmtpClient("smtpdm.aliyun.com", 25);

                //C#官方文档介绍说明不支持隐式TLS方式，即465端口，需要使用25或者80端口(ECS不支持25端口)，另外需增加一行 smtpClient.EnableSsl = true; 故使用SMTP加密方式需要修改如下：
                //SmtpClient smtpClient = new SmtpClient("smtpdm.aliyun.com", 80);
                //smtpClient.EnableSsl = true;
                // 使用SMTP用户名和密码进行验证
                System.Net.NetworkCredential credentials = new System.Net.NetworkCredential(Options.UserName, Options.Password);
                smtpClient.Credentials = credentials;
                smtpClient.Send(mailMsg);
            }
            catch (System.Exception ex)
            {
                _logger.LogError(ex.ToString());
                throw;
            }

        }

        private void Execute(string subject, string message, string email, string receiver)
        {
            try
            {
                MailMessage mailMsg = new MailMessage();

                mailMsg.To.Add(new MailAddress(receiver));

                mailMsg.From = new MailAddress(Options.FromAddress, Options.FromDisplayName);

                mailMsg.Subject = subject + "[" + email + "]";

                // 邮件正文内容
                if (Options.BodyIsPlain)
                {
                    mailMsg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(message, null, MediaTypeNames.Text.Plain));
                }
                else
                {
                    mailMsg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(message, null, MediaTypeNames.Text.Html));
                }

                //邮件推送的SMTP地址和端口
                SmtpClient smtpClient = new SmtpClient("smtpdm.aliyun.com", 25);

                //C#官方文档介绍说明不支持隐式TLS方式，即465端口，需要使用25或者80端口(ECS不支持25端口)，另外需增加一行 smtpClient.EnableSsl = true; 故使用SMTP加密方式需要修改如下：
                //SmtpClient smtpClient = new SmtpClient("smtpdm.aliyun.com", 80);
                //smtpClient.EnableSsl = true;
                // 使用SMTP用户名和密码进行验证
                System.Net.NetworkCredential credentials = new System.Net.NetworkCredential(Options.UserName, Options.Password);
                smtpClient.Credentials = credentials;
                smtpClient.Send(mailMsg);
            }
            catch (System.Exception ex)
            {
                _logger.LogError(ex.ToString());
                throw;
            }


        }

    }



}
